package com.transitionseverywhere;

import android.graphics.Path;
import android.util.Log;
import java.util.ArrayList;
import java.util.Arrays;

/* loaded from: classes.dex */
public class PathParser {
    static final String LOGTAG = PathParser.class.getSimpleName();

    public static Path createPathFromPathData(String str) {
        Path path = new Path();
        PathDataNode[] createNodesFromPathData = createNodesFromPathData(str);
        if (createNodesFromPathData != null) {
            PathDataNode.nodesToPath(createNodesFromPathData, path);
            return path;
        }
        return null;
    }

    public static PathDataNode[] createNodesFromPathData(String str) {
        if (str == null) {
            return null;
        }
        ArrayList arrayList = new ArrayList();
        int i = 1;
        int i2 = 0;
        while (i < str.length()) {
            int nextStart = nextStart(str, i);
            String trim = str.substring(i2, nextStart).trim();
            if (trim.length() > 0) {
                addNode(arrayList, trim.charAt(0), getFloats(trim));
            }
            i2 = nextStart;
            i = nextStart + 1;
        }
        if (i - i2 == 1 && i2 < str.length()) {
            addNode(arrayList, str.charAt(i2), new float[0]);
        }
        return (PathDataNode[]) arrayList.toArray(new PathDataNode[arrayList.size()]);
    }

    public static PathDataNode[] deepCopyNodes(PathDataNode[] pathDataNodeArr) {
        if (pathDataNodeArr == null) {
            return null;
        }
        PathDataNode[] pathDataNodeArr2 = new PathDataNode[pathDataNodeArr.length];
        for (int i = 0; i < pathDataNodeArr.length; i++) {
            pathDataNodeArr2[i] = new PathDataNode(pathDataNodeArr[i]);
        }
        return pathDataNodeArr2;
    }

    public static boolean canMorph(PathDataNode[] pathDataNodeArr, PathDataNode[] pathDataNodeArr2) {
        if (pathDataNodeArr == null || pathDataNodeArr2 == null || pathDataNodeArr.length != pathDataNodeArr2.length) {
            return false;
        }
        for (int i = 0; i < pathDataNodeArr.length; i++) {
            if (pathDataNodeArr[i].mType != pathDataNodeArr2[i].mType || pathDataNodeArr[i].mParams.length != pathDataNodeArr2[i].mParams.length) {
                return false;
            }
        }
        return true;
    }

    public static void updateNodes(PathDataNode[] pathDataNodeArr, PathDataNode[] pathDataNodeArr2) {
        for (int i = 0; i < pathDataNodeArr2.length; i++) {
            pathDataNodeArr[i].mType = pathDataNodeArr2[i].mType;
            for (int i2 = 0; i2 < pathDataNodeArr2[i].mParams.length; i2++) {
                pathDataNodeArr[i].mParams[i2] = pathDataNodeArr2[i].mParams[i2];
            }
        }
    }

    private static int nextStart(String str, int i) {
        while (i < str.length()) {
            char charAt = str.charAt(i);
            if ((charAt - 'A') * (charAt - 'Z') <= 0 || (charAt - 'a') * (charAt - 'z') <= 0) {
                break;
            }
            i++;
        }
        return i;
    }

    private static void addNode(ArrayList<PathDataNode> arrayList, char c, float[] fArr) {
        arrayList.add(new PathDataNode(c, fArr));
    }

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes.dex */
    public static class ExtractFloatResult {
        int mEndPosition;
        boolean mEndWithNegSign;

        private ExtractFloatResult() {
        }
    }

    private static float[] getFloats(String str) {
        int i = 0;
        int i2 = 1;
        if ((str.charAt(0) == 'z') || (str.charAt(0) == 'Z')) {
            return new float[0];
        }
        try {
            float[] fArr = new float[str.length()];
            ExtractFloatResult extractFloatResult = new ExtractFloatResult();
            int length = str.length();
            while (i2 < length) {
                extract(str, i2, extractFloatResult);
                int i3 = extractFloatResult.mEndPosition;
                if (i2 < i3) {
                    fArr[i] = Float.parseFloat(str.substring(i2, i3));
                    i++;
                }
                i2 = extractFloatResult.mEndWithNegSign ? i3 : i3 + 1;
            }
            return Arrays.copyOf(fArr, i);
        } catch (NumberFormatException e) {
            String str2 = LOGTAG;
            Log.e(str2, "error in parsing \"" + str + "\"");
            throw e;
        }
    }

    /* JADX WARN: Removed duplicated region for block: B:17:0x0025 A[LOOP:0: B:3:0x0005->B:17:0x0025, LOOP_END] */
    /* JADX WARN: Removed duplicated region for block: B:20:0x0028 A[EDGE_INSN: B:20:0x0028->B:18:0x0028 ?: BREAK  , SYNTHETIC] */
    /*
        Code decompiled incorrectly, please refer to instructions dump.
        To view partially-correct code enable 'Show inconsistent code' option in preferences
    */
    private static void extract(java.lang.String r5, int r6, com.transitionseverywhere.PathParser.ExtractFloatResult r7) {
        /*
            r0 = 0
            r7.mEndWithNegSign = r0
            r0 = r6
            r1 = 0
        L5:
            int r2 = r5.length()
            if (r0 >= r2) goto L28
            char r2 = r5.charAt(r0)
            r3 = 32
            r4 = 1
            if (r2 == r3) goto L21
            r3 = 44
            if (r2 == r3) goto L21
            r3 = 45
            if (r2 == r3) goto L1d
            goto L22
        L1d:
            if (r0 == r6) goto L22
            r7.mEndWithNegSign = r4
        L21:
            r1 = 1
        L22:
            if (r1 == 0) goto L25
            goto L28
        L25:
            int r0 = r0 + 1
            goto L5
        L28:
            r7.mEndPosition = r0
            return
        */
        throw new UnsupportedOperationException("Method not decompiled: com.transitionseverywhere.PathParser.extract(java.lang.String, int, com.transitionseverywhere.PathParser$ExtractFloatResult):void");
    }

    /* loaded from: classes.dex */
    public static class PathDataNode {
        private float[] mParams;
        private char mType;

        private PathDataNode(char c, float[] fArr) {
            this.mType = c;
            this.mParams = fArr;
        }

        private PathDataNode(PathDataNode pathDataNode) {
            this.mType = pathDataNode.mType;
            float[] fArr = pathDataNode.mParams;
            this.mParams = Arrays.copyOf(fArr, fArr.length);
        }

        public static void nodesToPath(PathDataNode[] pathDataNodeArr, Path path) {
            float[] fArr = new float[4];
            char c = 'm';
            for (int i = 0; i < pathDataNodeArr.length; i++) {
                addCommand(path, fArr, c, pathDataNodeArr[i].mType, pathDataNodeArr[i].mParams);
                c = pathDataNodeArr[i].mType;
            }
        }

        public void interpolatePathDataNode(PathDataNode pathDataNode, PathDataNode pathDataNode2, float f) {
            int i = 0;
            while (true) {
                float[] fArr = pathDataNode.mParams;
                if (i >= fArr.length) {
                    return;
                }
                this.mParams[i] = (fArr[i] * (1.0f - f)) + (pathDataNode2.mParams[i] * f);
                i++;
            }
        }

        private static void addCommand(Path path, float[] fArr, char c, char c2, float[] fArr2) {
            int i;
            int i2;
            float f;
            float f2;
            float f3;
            float f4;
            float f5;
            float f6;
            float f7;
            float f8;
            float f9;
            float f10;
            float f11;
            float f12;
            float f13;
            float f14;
            Path path2 = path;
            float f15 = fArr[0];
            float f16 = fArr[1];
            float f17 = fArr[2];
            float f18 = fArr[3];
            switch (c2) {
                case 'A':
                case 'a':
                    i = 7;
                    break;
                case 'C':
                case 'c':
                    i = 6;
                    break;
                case 'H':
                case 'V':
                case 'h':
                case 'v':
                    i = 1;
                    break;
                case 'L':
                case 'M':
                case 'T':
                case 'l':
                case 'm':
                case 't':
                default:
                    i = 2;
                    break;
                case 'Q':
                case 'S':
                case 'q':
                case 's':
                    i = 4;
                    break;
                case 'Z':
                case 'z':
                    path.close();
                    return;
            }
            float f19 = f15;
            float f20 = f16;
            int i3 = 0;
            char c3 = c;
            while (i3 < fArr2.length) {
                float f21 = 0.0f;
                switch (c2) {
                    case 'A':
                        i2 = i3;
                        int i4 = i2 + 5;
                        int i5 = i2 + 6;
                        drawArc(path, f19, f20, fArr2[i4], fArr2[i5], fArr2[i2 + 0], fArr2[i2 + 1], fArr2[i2 + 2], fArr2[i2 + 3] != 0.0f, fArr2[i2 + 4] != 0.0f);
                        f19 = fArr2[i4];
                        f20 = fArr2[i5];
                        f18 = f20;
                        f17 = f19;
                        break;
                    case 'C':
                        i2 = i3;
                        int i6 = i2 + 2;
                        int i7 = i2 + 3;
                        int i8 = i2 + 4;
                        int i9 = i2 + 5;
                        path.cubicTo(fArr2[i2 + 0], fArr2[i2 + 1], fArr2[i6], fArr2[i7], fArr2[i8], fArr2[i9]);
                        f19 = fArr2[i8];
                        float f22 = fArr2[i9];
                        float f23 = fArr2[i6];
                        float f24 = fArr2[i7];
                        f20 = f22;
                        f18 = f24;
                        f17 = f23;
                        break;
                    case 'H':
                        i2 = i3;
                        int i10 = i2 + 0;
                        path2.lineTo(fArr2[i10], f20);
                        f19 = fArr2[i10];
                        break;
                    case 'L':
                        i2 = i3;
                        int i11 = i2 + 0;
                        int i12 = i2 + 1;
                        path2.lineTo(fArr2[i11], fArr2[i12]);
                        f19 = fArr2[i11];
                        f20 = fArr2[i12];
                        break;
                    case 'M':
                        i2 = i3;
                        int i13 = i2 + 0;
                        int i14 = i2 + 1;
                        path2.moveTo(fArr2[i13], fArr2[i14]);
                        f19 = fArr2[i13];
                        f20 = fArr2[i14];
                        break;
                    case 'Q':
                        i2 = i3;
                        int i15 = i2 + 0;
                        int i16 = i2 + 1;
                        int i17 = i2 + 2;
                        int i18 = i2 + 3;
                        path2.quadTo(fArr2[i15], fArr2[i16], fArr2[i17], fArr2[i18]);
                        f = fArr2[i15];
                        f2 = fArr2[i16];
                        f19 = fArr2[i17];
                        f20 = fArr2[i18];
                        f17 = f;
                        f18 = f2;
                        break;
                    case 'S':
                        float f25 = f20;
                        float f26 = f19;
                        i2 = i3;
                        if (c3 == 'c' || c3 == 's' || c3 == 'C' || c3 == 'S') {
                            float f27 = (f26 * 2.0f) - f17;
                            f3 = (f25 * 2.0f) - f18;
                            f4 = f27;
                        } else {
                            f4 = f26;
                            f3 = f25;
                        }
                        int i19 = i2 + 0;
                        int i20 = i2 + 1;
                        int i21 = i2 + 2;
                        int i22 = i2 + 3;
                        path.cubicTo(f4, f3, fArr2[i19], fArr2[i20], fArr2[i21], fArr2[i22]);
                        f = fArr2[i19];
                        f2 = fArr2[i20];
                        f19 = fArr2[i21];
                        f20 = fArr2[i22];
                        f17 = f;
                        f18 = f2;
                        break;
                    case 'T':
                        float f28 = f20;
                        float f29 = f19;
                        i2 = i3;
                        if (c3 == 'q' || c3 == 't' || c3 == 'Q' || c3 == 'T') {
                            f28 = (f28 * 2.0f) - f18;
                            f29 = (f29 * 2.0f) - f17;
                        }
                        int i23 = i2 + 0;
                        int i24 = i2 + 1;
                        path2.quadTo(f29, f28, fArr2[i23], fArr2[i24]);
                        f19 = fArr2[i23];
                        f5 = fArr2[i24];
                        f17 = f29;
                        f18 = f28;
                        f20 = f5;
                        break;
                    case 'V':
                        float f30 = f19;
                        i2 = i3;
                        int i25 = i2 + 0;
                        path2 = path;
                        path2.lineTo(f30, fArr2[i25]);
                        f5 = fArr2[i25];
                        f19 = f30;
                        f20 = f5;
                        break;
                    case 'Z':
                    case 'z':
                        f6 = f20;
                        i2 = i3;
                        path.close();
                        f7 = f19;
                        path2 = path;
                        f19 = f7;
                        f20 = f6;
                        break;
                    case 'a':
                        int i26 = i3 + 5;
                        float f31 = fArr2[i26] + f19;
                        int i27 = i3 + 6;
                        float f32 = fArr2[i27] + f20;
                        float f33 = fArr2[i3 + 0];
                        float f34 = fArr2[i3 + 1];
                        float f35 = fArr2[i3 + 2];
                        float f36 = f19;
                        boolean z = fArr2[i3 + 3] != 0.0f;
                        i2 = i3;
                        drawArc(path, f19, f20, f31, f32, f33, f34, f35, z, fArr2[i3 + 4] != 0.0f);
                        f19 = f36 + fArr2[i26];
                        f20 += fArr2[i27];
                        path2 = path;
                        f18 = f20;
                        f17 = f19;
                        break;
                    case 'c':
                        int i28 = i3 + 2;
                        int i29 = i3 + 3;
                        int i30 = i3 + 4;
                        int i31 = i3 + 5;
                        path.rCubicTo(fArr2[i3 + 0], fArr2[i3 + 1], fArr2[i28], fArr2[i29], fArr2[i30], fArr2[i31]);
                        f8 = fArr2[i28] + f19;
                        f9 = fArr2[i29] + f20;
                        f19 += fArr2[i30];
                        f10 = fArr2[i31];
                        f20 += f10;
                        f17 = f8;
                        f18 = f9;
                        i2 = i3;
                        break;
                    case 'h':
                        int i32 = i3 + 0;
                        path2.rLineTo(fArr2[i32], 0.0f);
                        f19 += fArr2[i32];
                        i2 = i3;
                        break;
                    case 'l':
                        int i33 = i3 + 0;
                        int i34 = i3 + 1;
                        path2.rLineTo(fArr2[i33], fArr2[i34]);
                        f19 += fArr2[i33];
                        f11 = fArr2[i34];
                        f20 += f11;
                        i2 = i3;
                        break;
                    case 'm':
                        int i35 = i3 + 0;
                        int i36 = i3 + 1;
                        path2.rMoveTo(fArr2[i35], fArr2[i36]);
                        f19 += fArr2[i35];
                        f11 = fArr2[i36];
                        f20 += f11;
                        i2 = i3;
                        break;
                    case 'q':
                        int i37 = i3 + 0;
                        int i38 = i3 + 1;
                        int i39 = i3 + 2;
                        int i40 = i3 + 3;
                        path2.rQuadTo(fArr2[i37], fArr2[i38], fArr2[i39], fArr2[i40]);
                        f8 = fArr2[i37] + f19;
                        f9 = fArr2[i38] + f20;
                        f19 += fArr2[i39];
                        f10 = fArr2[i40];
                        f20 += f10;
                        f17 = f8;
                        f18 = f9;
                        i2 = i3;
                        break;
                    case 's':
                        if (c3 == 'c' || c3 == 's' || c3 == 'C' || c3 == 'S') {
                            float f37 = f19 - f17;
                            f12 = f20 - f18;
                            f13 = f37;
                        } else {
                            f13 = 0.0f;
                            f12 = 0.0f;
                        }
                        int i41 = i3 + 0;
                        int i42 = i3 + 1;
                        int i43 = i3 + 2;
                        int i44 = i3 + 3;
                        path.rCubicTo(f13, f12, fArr2[i41], fArr2[i42], fArr2[i43], fArr2[i44]);
                        f8 = fArr2[i41] + f19;
                        f9 = fArr2[i42] + f20;
                        f19 += fArr2[i43];
                        f10 = fArr2[i44];
                        f20 += f10;
                        f17 = f8;
                        f18 = f9;
                        i2 = i3;
                        break;
                    case 't':
                        if (c3 == 'q' || c3 == 't' || c3 == 'Q' || c3 == 'T') {
                            f21 = f19 - f17;
                            f14 = f20 - f18;
                        } else {
                            f14 = 0.0f;
                        }
                        int i45 = i3 + 0;
                        int i46 = i3 + 1;
                        path2.rQuadTo(f21, f14, fArr2[i45], fArr2[i46]);
                        float f38 = f21 + f19;
                        float f39 = f14 + f20;
                        f19 += fArr2[i45];
                        f20 += fArr2[i46];
                        f18 = f39;
                        i2 = i3;
                        f17 = f38;
                        break;
                    case 'v':
                        int i47 = i3 + 0;
                        path2.rLineTo(0.0f, fArr2[i47]);
                        f11 = fArr2[i47];
                        f20 += f11;
                        i2 = i3;
                        break;
                    default:
                        f6 = f20;
                        f7 = f19;
                        i2 = i3;
                        f19 = f7;
                        f20 = f6;
                        break;
                }
                i3 = i2 + i;
                c3 = c2;
            }
            fArr[0] = f19;
            fArr[1] = f20;
            fArr[2] = f17;
            fArr[3] = f18;
        }

        private static void drawArc(Path path, float f, float f2, float f3, float f4, float f5, float f6, float f7, boolean z, boolean z2) {
            double d;
            double d2;
            double radians = Math.toRadians(f7);
            double cos = Math.cos(radians);
            double sin = Math.sin(radians);
            double d3 = f;
            Double.isNaN(d3);
            double d4 = d3 * cos;
            double d5 = f2;
            Double.isNaN(d5);
            double d6 = f5;
            Double.isNaN(d6);
            double d7 = (d4 + (d5 * sin)) / d6;
            double d8 = -f;
            Double.isNaN(d8);
            Double.isNaN(d5);
            double d9 = f6;
            Double.isNaN(d9);
            double d10 = ((d8 * sin) + (d5 * cos)) / d9;
            double d11 = f3;
            Double.isNaN(d11);
            double d12 = f4;
            Double.isNaN(d12);
            Double.isNaN(d6);
            double d13 = ((d11 * cos) + (d12 * sin)) / d6;
            double d14 = -f3;
            Double.isNaN(d14);
            Double.isNaN(d12);
            Double.isNaN(d9);
            double d15 = ((d14 * sin) + (d12 * cos)) / d9;
            double d16 = d7 - d13;
            double d17 = d10 - d15;
            double d18 = (d7 + d13) / 2.0d;
            double d19 = (d10 + d15) / 2.0d;
            double d20 = (d16 * d16) + (d17 * d17);
            if (d20 == 0.0d) {
                Log.w(PathParser.LOGTAG, " Points are coincident");
                return;
            }
            double d21 = (1.0d / d20) - 0.25d;
            if (d21 < 0.0d) {
                Log.w(PathParser.LOGTAG, "Points are too far apart " + d20);
                float sqrt = (float) (Math.sqrt(d20) / 1.99999d);
                drawArc(path, f, f2, f3, f4, f5 * sqrt, f6 * sqrt, f7, z, z2);
                return;
            }
            double sqrt2 = Math.sqrt(d21);
            double d22 = d16 * sqrt2;
            double d23 = sqrt2 * d17;
            if (z == z2) {
                d = d18 - d23;
                d2 = d19 + d22;
            } else {
                d = d18 + d23;
                d2 = d19 - d22;
            }
            double atan2 = Math.atan2(d10 - d2, d7 - d);
            double atan22 = Math.atan2(d15 - d2, d13 - d) - atan2;
            if (z2 != (atan22 >= 0.0d)) {
                atan22 = atan22 > 0.0d ? atan22 - 6.283185307179586d : atan22 + 6.283185307179586d;
            }
            Double.isNaN(d6);
            double d24 = d * d6;
            Double.isNaN(d9);
            double d25 = d2 * d9;
            arcToBezier(path, (d24 * cos) - (d25 * sin), (d24 * sin) + (d25 * cos), d6, d9, d3, d5, radians, atan2, atan22);
        }

        private static void arcToBezier(Path path, double d, double d2, double d3, double d4, double d5, double d6, double d7, double d8, double d9) {
            double d10 = d3;
            int abs = Math.abs((int) Math.ceil((d9 * 4.0d) / 3.141592653589793d));
            double cos = Math.cos(d7);
            double sin = Math.sin(d7);
            double cos2 = Math.cos(d8);
            double sin2 = Math.sin(d8);
            double d11 = -d10;
            double d12 = d11 * cos;
            double d13 = d4 * sin;
            double d14 = (d12 * sin2) - (d13 * cos2);
            double d15 = d11 * sin;
            double d16 = d4 * cos;
            double d17 = (sin2 * d15) + (cos2 * d16);
            double d18 = abs;
            Double.isNaN(d18);
            double d19 = d9 / d18;
            double d20 = d5;
            double d21 = d6;
            double d22 = d17;
            double d23 = d14;
            int i = 0;
            double d24 = d8;
            while (i < abs) {
                double d25 = d24 + d19;
                double sin3 = Math.sin(d25);
                double cos3 = Math.cos(d25);
                double d26 = (d + ((d10 * cos) * cos3)) - (d13 * sin3);
                double d27 = d2 + (d10 * sin * cos3) + (d16 * sin3);
                double d28 = (d12 * sin3) - (d13 * cos3);
                double d29 = (sin3 * d15) + (cos3 * d16);
                double d30 = d25 - d24;
                double tan = Math.tan(d30 / 2.0d);
                double sin4 = (Math.sin(d30) * (Math.sqrt(((tan * 3.0d) * tan) + 4.0d) - 1.0d)) / 3.0d;
                path.cubicTo((float) (d20 + (d23 * sin4)), (float) (d21 + (d22 * sin4)), (float) (d26 - (sin4 * d28)), (float) (d27 - (sin4 * d29)), (float) d26, (float) d27);
                i++;
                d19 = d19;
                abs = abs;
                sin = sin;
                d21 = d27;
                d15 = d15;
                d24 = d25;
                d22 = d29;
                d23 = d28;
                cos = cos;
                d10 = d3;
                d20 = d26;
            }
        }
    }
}
